//
//  YSScrollLabel.m
//  YSKitExample
//
//  Created by Kyson on 2017/6/16.
//  Copyright © 2017年 YangShen. All rights reserved.
//

#import "YSScrollLabel.h"
#import "YSWeakProxy.h"
#import "UIView+YSAdd.h"

@interface YSScrollLabel ()
@property (nonatomic, strong) UILabel *headLabel;
@property (nonatomic, strong) UILabel *tailLabel;

@property (nonatomic, assign) BOOL isScrolling;
@end

@implementation YSScrollLabel {
    NSTimer *_timer;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    _direction = YSScrollDirectionHorizontal;
    _scrollSpace = 10;
    _labelInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.headLabel = [[UILabel alloc] init];
    [self addSubview:_headLabel];
    self.tailLabel = [[UILabel alloc] init];
    [self addSubview:_tailLabel];
    
    [self createTimer];
    
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pauseScrolling)]];
}

- (void)setScrollVelocity:(NSTimeInterval)scrollVelocity {
    _scrollVelocity = scrollVelocity;
    [self createTimer];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _headLabel.frame = self.bounds;
    
    CGFloat lW = self.ys_width - _labelInset.left - _labelInset.right;
    CGFloat lH = self.ys_height - _labelInset.top - _labelInset.bottom;
    
    switch (_direction) {
        case YSScrollDirectionHorizontal: {
            CGSize size = [_headLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, lH) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil].size;
            size.width += 10;
            _headLabel.frame = CGRectMake(_labelInset.left, _labelInset.top, size.width, lH);
            self.contentSize = size;
            _tailLabel.frame = CGRectMake(_labelInset.left + size.width, _labelInset.top, size.width, lH);
        }
            break;
        case YSScrollDirectionVertical: {
            CGSize size = [_headLabel.text boundingRectWithSize:CGSizeMake(lW, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil].size;
            _headLabel.frame = CGRectMake(_labelInset.left, _labelInset.top, lW, size.height);
            self.contentSize = size;
            _tailLabel.frame = CGRectMake(_labelInset.left, size.height + _labelInset.top, lW, size.height);
        }
            break;
        default:
            break;
    }
}

- (void)scroll:(NSTimer *)timer {
    CGPoint p = self.contentOffset;
    switch (_direction) {
        case YSScrollDirectionHorizontal: {
            if (p.x >= self.contentSize.width) {
                p.x = self.contentOffset.x - self.contentSize.width + _scrollSpace;
            } else {
                p.x += _scrollSpace;
            }
        }
            break;
        case YSScrollDirectionVertical: {
            if (p.y >= self.contentSize.height) {
                p.y = _scrollSpace + self.contentOffset.y - self.contentSize.height;
            } else {
                p.y += _scrollSpace;
            }
        }
            break;
        default:
            break;
    }
    self.contentOffset = p;
}

- (void)pauseScrolling {
    if (_isScrolling) {
        [_timer setFireDate:[NSDate distantFuture]];
        _isScrolling = !_isScrolling;
        return;
    }
    [_timer setFireDate:[NSDate distantPast]];
    _isScrolling = !_isScrolling;
}

- (void)beginScrolling {
    if (_isScrolling) {
        return;
    }
    if (_timer) {
        [self pauseScrolling];
        return;
    }
    [self createTimer];
}

- (void)endScrolling {
    [_timer invalidate];
    _timer = nil;
    _isScrolling = NO;
}

// 暂停
- (void)pauseScrollings {
    [_timer setFireDate:[NSDate distantFuture]];
}

- (void)createTimer {
    if(_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    NSTimeInterval s = _scrollVelocity ? _scrollVelocity : 0.3;
    _timer = [NSTimer timerWithTimeInterval:s target:[YSWeakProxy proxyWithTarget:self] selector:@selector(scroll:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    _isScrolling = YES;
}

#pragma mark - Setter
- (void)setText:(NSString *)text {
    _text = text;
    _headLabel.text = text;
    _tailLabel.text = text;
}

- (void)setFont:(UIFont *)font {
    _font = font;
    _headLabel.font = font;
    _tailLabel.font = font;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    _headLabel.textColor = textColor;
    _tailLabel.textColor = textColor;
}

- (void)setDirection:(YSScrollDirection)direction {
    _direction = direction;
    if (YSScrollDirectionVertical == direction) {
        _headLabel.numberOfLines = 0;
        _tailLabel.numberOfLines = 0;
    }
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    _attributedText = attributedText;
    _headLabel.attributedText = attributedText;
}

@end
